package com.yenlien.traceyun.breed.controller;

import com.alibaba.fastjson.JSONArray;
import com.yenlien.traceyun.breed.entity.Breed;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.breed.entity.BreedDetail;
import com.yenlien.traceyun.breed.service.BreedDetailService;
import com.yenlien.traceyun.business.interceptor.DataAuth;
import com.yenlien.traceyun.webase.controller.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/breedDetail")
public class BreedDetailController extends BaseController {
    @Autowired
    private BreedDetailService breedDetailService;

    //分页查询列表
    @DataAuth
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody JSONObject params) {
        BreedDetail breedDetail = new BreedDetail();
        breedDetail.setRelevanceId(params.getString("relevanceId"));
        breedDetail.setSource(params.getString("source"));
        breedDetail.setCode(params.getString("searchName"));
        breedDetail.setEquipmentName(params.getString("searchName"));
        breedDetail.setPage(params.getIntValue("page"));
        return buildPage(breedDetailService.findByList(breedDetail));
    }

    /**
     * 批量查询
     *
     * @param params
     * @return
     */
    @DataAuth
    @RequestMapping(value = "/findByRfids", method = RequestMethod.POST)
    public Object findByRfids(@RequestBody JSONObject params) {
        JSONObject result = new JSONObject();
        JSONArray rfids = params.getJSONArray("rfids");
        List<String> fails = new ArrayList<>();
        if (rfids == null || rfids.size() == 0) {
            result.put("details", new ArrayList<>());
            result.put("fails", fails);
            return result;
        }
        List<BreedDetail> details = breedDetailService.findByRfids(rfids.toJavaList(String.class));

        if (details != null && details.size() > 0) {
            for (int i = 0; i < rfids.size(); i++) {
                String item = (rfids.getString(i));
                boolean exist = false;
                for (BreedDetail detail : details) {
                    if (item.equals(detail.getCode())) {
                        exist = true;
                        break;
                    }
                }
                if (!exist) {
                    fails.add(item);
                }
            }
        } else {
            result.put("fails", rfids);
        }
        result.put("details", details);
        return result;
    }

    //根据养殖批次号查询母源信息
    @DataAuth
    @RequestMapping(value = "/findByBatch/{batch}", method = RequestMethod.GET)
    public Object findByBatch(@PathVariable("batch") String batch, HttpServletRequest request) {
        String isout = request.getParameter("isout");
        boolean out = false;
        if (isout != null && isout.equals("1")) {
            out = true;
        }
        return success(breedDetailService.findByBatch(batch, out));
    }
}
